﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace smallDeimos.Game
{
    public partial class PlayerSettings : System.Web.UI.Page
    {
        int UserId;
        string UserName;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Request.Cookies["sd_id"] != null)
            {
                HttpCookie cookie = Request.Cookies["sd_user"];
                UserName = cookie.Value;
                HttpCookie cookie2 = Request.Cookies["sd_id"];
                UserId = int.Parse(cookie2.Value);
                int TimeZone = UserDefinedFunctions.GetPlayerInt(UserId, "TimeZone").Value;
                if (this.Page.IsPostBack == false)
                    TextBox1.Text = TimeZone.ToString();
            }
            else
            {
                Session["Error"] = "Cookie sd_id doesn't exists";
                Response.Redirect("~/Error.aspx");
                return;
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            StoredProcedures.ResignPlayer(UserId);
            Page.Response.Redirect("~/");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            bool saved = false;
            int t;
            if (int.TryParse(TextBox1.Text, out t) == true)
            {
                StoredProcedures.SaveInt(UserId, "TimeZone", int.Parse(TextBox1.Text));
                saved=true;
            }
            if (TextBox2.Text != null && TextBox2.Text != string.Empty )
            {
                if (UserDefinedFunctions.AuthorizeUser(UserName, TextBox2.Text) == false)
                {
                    Label1.Text = "You must provide valid password to change your password.";
                    return;
                }
                UserDefinedFunctions.ChangePassword(UserId, TextBox3.Text);
            }
            if (saved == true)
            {
                Label1.Text = "Settings saved";
                Page.Response.Redirect(Page.Request.Url.ToString());
            }
            else
                Label1.Text = "Cannot save settings";
        }
    }
}
